package com.application.lucky.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.application.lucky.BuildConfig;
import com.application.lucky.Lucky;
import com.application.lucky.R;
import com.application.lucky.activity.LoginActivity;
import com.application.lucky.bean.jsonBean.GroupListbean;
import com.application.lucky.manager.GroupDataManager;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.utils.TopActivityManager;
import com.application.lucky.widget.TitlebarView;
import com.application.lucky.widget.dialog.TwoButtonDialog;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.SharedPreferencesUtil;
import com.application.mylibrary.utils.ToastUtils;
import com.qunar.im.protobuf.common.CurrentPreference;
import com.qunar.im.utils.ConnectionUtil;
import com.qunar.im.utils.IMPreferencesUtil;
import com.suke.widget.SwitchButton;

import java.util.ArrayList;

public class SettingLuckyActivity extends BaseActivity {
    private TitlebarView mTitleBar;

    private Button btn_logout;
    private View layout_cache;
    private TextView tv_version;

    private SwitchButton sb_gift;
    private SwitchButton sb_message;
    private SwitchButton sb_key;
    private TwoButtonDialog logoutDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_lucky_setting;
    }

    private void initView() {
        mTitleBar = (TitlebarView) findViewById(R.id.title_bar);
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });

        btn_logout = findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutDialog();
            }
        });
        btn_logout.setVisibility(View.VISIBLE);
        layout_cache = findViewById(R.id.layout_cache);
        layout_cache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("清除成功");
            }
        });
        tv_version = findViewById(R.id.tv_version);
        tv_version.setText(BuildConfig.VERSION_NAME);
        sb_gift = findViewById(R.id.sb_gift);
        sb_message = findViewById(R.id.sb_message);
        sb_key = findViewById(R.id.sb_key);

        sb_gift.setChecked(SharedPreferencesUtil.getBoolean(getApplicationContext(), IMPreferencesUtil.PreferencesKey.KEY_GIFT_VOICE, true));
        sb_message.setChecked(CurrentPreference.getInstance().isTurnOnMsgSound());
        sb_key.setChecked(true);

        sb_gift.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                SharedPreferencesUtil.saveBoolean(getApplicationContext(), IMPreferencesUtil.PreferencesKey.KEY_GIFT_VOICE, isChecked);
            }
        });

        sb_message.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                CurrentPreference.getInstance().setTurnOnMsgSound(isChecked);
            }
        });

        sb_key.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                // TODO 后期编写按键音的控制逻辑
            }
        });

    }

    private void showLogoutDialog() {
        logoutDialog = new TwoButtonDialog.Build(SettingLuckyActivity.this)
                .disMiss(true)
                .setMessage("确定退出登录？")
                .setLeftButton("取消", null)
                .setRightButton("确定", new TwoButtonDialog.BaseDialogClickListener() {
                    @Override
                    public void click() {
                        logoutDialog.dismiss();

                        UserManagers.getInstance().loginOut();
                        TopActivityManager.getInstance().exit();
                        Lucky.getApplication().getDaoSession().getLateContactBeanDao().deleteAll();
                        Lucky.getApplication().getDaoSession().getGroupListbeanDao().deleteAll();
                        Lucky.getApplication().getDaoSession().getFriendSearchBeanDao().deleteAll();
                        SharedPreferencesUtil.clear(Lucky.getApplication());
                        GroupDataManager.getInstance().setDataInfo(new ArrayList<GroupListbean>());
                        ConnectionUtil.getInstance().DeleteSessionList();

                        Intent login = new Intent(SettingLuckyActivity.this, LoginActivity.class);
                        login.putExtra("isout", true);
                        login.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(login);
                    }
                }).create();
        logoutDialog.show();
    }
}
